﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using iTeachSoft.CoreObjects;
using System.Windows.Input;
using iTeachSoft.Presentation.Dialogs;
using System.Windows.Controls;
using System.Windows;

namespace iTeachSoft.Presentation.Code.Wrappers
{
    public class TextAnnotationShpae : BasicShape
    {
        #region "Dependency properties"

        public double RadiusX
        {
            get { return (double)GetValue(RadiusXProperty); }
            set { SetValue(RadiusXProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RadiusX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusXProperty =
            DependencyProperty.Register("RadiusX", typeof(double), typeof(TextAnnotationShpae), null);

        public double RadiusY
        {
            get { return (double)GetValue(RadiusYProperty); }
            set { SetValue(RadiusYProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RadiusY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusYProperty =
            DependencyProperty.Register("RadiusY", typeof(double), typeof(TextAnnotationShpae), null);


        public double BorderWeight
        {
            get { return (double)GetValue(BorderWeightProperty); }
            set { SetValue(BorderWeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BorderWeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BorderWeightProperty =
            DependencyProperty.Register("BorderWeight", typeof(double), typeof(TextAnnotationShpae), null);


        public Brush BackColor
        {
            get { return (Brush)GetValue(BackColorProperty); }
            set { SetValue(BackColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BackgroundColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackColorProperty =
            DependencyProperty.Register("BackColor", typeof(Brush), typeof(TextAnnotationShpae), new UIPropertyMetadata(new SolidColorBrush(Colors.Red)));


        public Brush StrokeColor
        {
            get { return (Brush)GetValue(StrokeColorProperty); }
            set { SetValue(StrokeColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StrokeColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StrokeColorProperty =
            DependencyProperty.Register("StrokeColor", typeof(Brush), typeof(TextAnnotationShpae), null);

        
        public double DepFontSize
        {
            get { return (double)GetValue(DepFontSizeProperty); }
            set { SetValue(DepFontSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FontSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DepFontSizeProperty =
            DependencyProperty.Register("DepFontSize", typeof(double), typeof(TextAnnotationShpae), new UIPropertyMetadata((double)10));
        
        public FontFamily DepFontFamily
        {
            get { return (FontFamily)GetValue(DepFontFamilyProperty); }
            set { SetValue(DepFontFamilyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FontFamily.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DepFontFamilyProperty =
            DependencyProperty.Register("DepFontFamily", typeof(FontFamily), typeof(TextAnnotationShpae), null);


        public Brush FontColor
        {
            get { return (Brush)GetValue(FontColorProperty); }
            set { SetValue(FontColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FontColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FontColorProperty =
            DependencyProperty.Register("FontColor", typeof(Brush), typeof(TextAnnotationShpae), new UIPropertyMetadata(new SolidColorBrush(Colors.Yellow)));


        public string ObjectText
        {
            get { return (string)GetValue(ObjectTextProperty); }
            set { SetValue(ObjectTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ObjectText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ObjectTextProperty =
            DependencyProperty.Register("ObjectText", typeof(string), typeof(TextAnnotationShpae), new UIPropertyMetadata(""));

        #endregion

        public RectangleWithText CoreObject
        {
            get { return mCoreObject as RectangleWithText; }
            set
            {
                mCoreObject = value;
                ReflectCoreObject();
            }
        }

        public TextAnnotationShpae()
            : base()
        {
            //default constructor
            this.Style = (Style)Application.Current.FindResource("TextAnnotationShapeStyle");
            this.MouseDoubleClick += new MouseButtonEventHandler(AnnotationShape_MouseDoubleClick);
        }

        public TextAnnotationShpae(RectangleWithText coreObject)
            : this()
        {
            CoreObject = coreObject;
        }

        private void ReflectCoreObject()
        {
            RadiusX = (double)CoreObject.RadiusX;
            RadiusY = (double)CoreObject.RadiusY;

            Canvas.SetLeft(this, CoreObject.Bounds.Left);
            Canvas.SetTop(this, CoreObject.Bounds.Top);

            this.Width = CoreObject.Bounds.Width;
            this.Height = CoreObject.Bounds.Height;

            BackColor = new SolidColorBrush(Color.FromArgb(CoreObject.BackColor.A, CoreObject.BackColor.R, CoreObject.BackColor.G, CoreObject.BackColor.B));
            StrokeColor = new SolidColorBrush(Color.FromArgb(CoreObject.Stroke.A, CoreObject.Stroke.R, CoreObject.Stroke.G, CoreObject.Stroke.B)); ;
            BorderWeight = (double)CoreObject.BorderWeight;

            DepFontFamily = new FontFamily(CoreObject.FontFamilyName);
            DepFontSize = (double)CoreObject.FontSize;
            FontColor = new SolidColorBrush(Color.FromArgb(CoreObject.FontColor.A, CoreObject.FontColor.R, CoreObject.FontColor.G, CoreObject.FontColor.B)); ;
            ObjectText = CoreObject.Text;
        }

        void AnnotationShape_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DialogRectangleWithText propertiesDialog = new DialogRectangleWithText();
            propertiesDialog.CoreObject = CoreObject;
            propertiesDialog.ShowDialog();

            ReflectCoreObject();
        }
    }
}
